package com.asurplus.weixin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.asurplus.common.layui.LayuiTableResult;
import com.asurplus.weixin.entity.WeixinSubscribeLog;
import com.asurplus.weixin.mapper.WeixinSubscribeLogMapper;
import com.asurplus.weixin.service.WeixinSubscribeLogService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 微信关注/取消关注日志表 服务实现类
 * </p>
 *
 * @author lizhou
 * @since 2020-11-06
 */
@Service
public class WeixinSubscribeLogServiceImpl extends ServiceImpl<WeixinSubscribeLogMapper, WeixinSubscribeLog> implements WeixinSubscribeLogService {

    @Override
    public void saveWeixinSubscribeLog(String openId, Integer type) {
        if (StringUtils.isBlank(openId)) {
            return;
        }
        WeixinSubscribeLog weixinSubscribeLog = new WeixinSubscribeLog();
        weixinSubscribeLog.setOpenId(openId);
        weixinSubscribeLog.setType(type);
        this.save(weixinSubscribeLog);
    }

    @Override
    public LayuiTableResult list(Integer page, Integer limit, WeixinSubscribeLog weixinSubscribeLog) {
        LambdaQueryWrapper<WeixinSubscribeLog> queryWrapper = new LambdaQueryWrapper<>();
        if (StringUtils.isNotBlank(weixinSubscribeLog.getOpenId())) {
            queryWrapper.eq(WeixinSubscribeLog::getOpenId, weixinSubscribeLog.getOpenId());
        }
        if (null != weixinSubscribeLog.getType()) {
            queryWrapper.eq(WeixinSubscribeLog::getType, weixinSubscribeLog.getType());
        }
        return new LayuiTableResult<>(this.page(new Page<>(page, limit), queryWrapper));
    }
}
